﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BaBaoFan.Modifier.Model;
using Dapper;

namespace BaBaoFan.Modifier.Data
{
    public class AlgorithmRepository : IAlgorithmRepository
    {
        private readonly IDbConnection _conn;
        public AlgorithmRepository(DbConnection conn)
        {
            _conn = conn.Connection;
        }

        public bool Add(CoinAlgorithm algorithm)
        {
            string sql = "insert into coin_algorithm (algorithm,isdel) values (@Algorithm,0)";
            return _conn.Execute(sql, algorithm) > 0;
        }

        public bool Delete(int[] ids)
        {
            string sql = "update coin_algorithm set isdel=1 where id in @ids";
            return _conn.Execute(sql, new { ids }) > 0;
        }

        public CoinAlgorithm GetCoinAlgorithmById(int id)
        {
            string sql = "select id as Id,algorithm as Algorithm from coin_algorithm where id=@id";
            return _conn.QuerySingle<CoinAlgorithm>(sql, new { id });
        }

        public IEnumerable<CoinAlgorithm> GetCoinAlgorithms()
        {
            string sql = "select id as Id,algorithm as Algorithm from coin_algorithm where isdel=0";
            return _conn.Query<CoinAlgorithm>(sql);
        }

        public bool Update(CoinAlgorithm algorithm)
        {
            string sql = "update coin_algorithm set algorithm=@Algorithm where id=@Id";
            return _conn.Execute(sql, algorithm)>0;
        }
        public IEnumerable<AlgorithmUnit> GetAlgorithmUnits() {
            string sql = "select id as Id,unit as Unit from hashrate_unit";
            return _conn.Query<AlgorithmUnit>(sql);
        }
    }
}
